﻿using DifyToOpenAI.Common;
using DifyToOpenAI.Model;
using Serilog;
using System.Runtime.CompilerServices;
using System.Text.Json;

namespace DifyToOpenAI
{
    public static class ApplicationContext
    {

        public static void Initialize()
        {
            /*模型列表初始化*/
            try
            {
                ModelListInitialize();
            }
            catch (Exception ex)
            {
                Log.ForContext("SourceContext", "ApplicationContext-ModelListInitialize").Fatal(ex.Message);
                // Console.WriteLine("ModelListInitialize ERROR: " + ex.Message);
            }
            /*全局配置初始化*/
            try
            {
                ConfigInitialize();
            }
            catch (Exception ex)
            {
                Log.ForContext("SourceContext", "ApplicationContext-ConfigInitialize").Fatal(ex.Message);
                throw;
            }

        }

        /// <summary>
        /// 模型列表初始化
        /// </summary>
        public static void ModelListInitialize()
        {
            // Console.WriteLine($"[诊断] 当前的 Logger 类型是: {Log.Logger.GetType().FullName}");
            #region 模型列表初始化
            string modellistjson = JsonAppConfigHelper.ReadConfigJson(JsonAppConfigHelper.ModelListPathFile);
            if (string.IsNullOrWhiteSpace(modellistjson))
            {
                //Console.WriteLine("modellistjson is error");
                Log.ForContext("SourceContext", "ApplicationContext").Fatal("modellistjson is empty");
            }
            ModelList modelList = new ModelList();
            modelList.RealJsonstring = modellistjson;
            initmodelList = modelList;
            if (initmodelList.data!.Count <= 0)
            {

                Log.ForContext("SourceContext", "ApplicationContext").Fatal("modellistjson config is error");

            }
            else
            {
                Log.ForContext("SourceContext", "ApplicationContext").Warning("modellistjson config is success");
            }

            #endregion
        }
        /// <summary>
        /// 全局配置Config初始化
        /// </summary>
        public static void ConfigInitialize()
        {
            AllConfigDic = JsonAppConfigHelper.ReadAllConfigByPath();

            string UseUnsafeRelaxedJsonEscaping = string.Empty;
            try
            {
                UseUnsafeRelaxedJsonEscaping = AllConfigDic["UseUnsafeRelaxedJsonEscaping"];
                if ("true".Equals(UseUnsafeRelaxedJsonEscaping, StringComparison.OrdinalIgnoreCase))
                {
                    jsonSerializerOptions.Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping;
                }
            }
            catch (Exception)
            {

                // throw;
            }

           string JsonWriteIndented=string.Empty;
            try
            {
                JsonWriteIndented = AllConfigDic["JsonWriteIndented"];
                if ("true".Equals(JsonWriteIndented, StringComparison.OrdinalIgnoreCase))
                {
                    jsonSerializerOptions.WriteIndented = true;
                }
            }
            catch (Exception)
            {

                throw;
            }
            string RequestTimeoutMillisecondsstr = string.Empty;
            try
            {
                RequestTimeoutMillisecondsstr = AllConfigDic["RequestTimeoutMilliseconds"];
                RequestTimeoutMilliseconds = Convert.ToInt32(RequestTimeoutMillisecondsstr);
            }
            catch (Exception)
            {

                // throw;
            }

            string OutputNodeInfostr=string.Empty;
            try
            {
                OutputNodeInfostr = AllConfigDic["OutputNodeInfo"];
                OutputNodeInfo = Convert.ToBoolean(OutputNodeInfostr);
            }
            catch (Exception)
            {

                // throw;
            }
        }
        /// <summary>
        /// 模型列表
        /// </summary>
        public static ModelList initmodelList { get; private set; } = new ModelList();

        /// <summary>
        /// Config配置读取
        /// </summary>
        public static Dictionary<string, string>? AllConfigDic { get; private set; }

        public static JsonSerializerOptions jsonSerializerOptions { get; private set; } = new JsonSerializerOptions();

        public static int RequestTimeoutMilliseconds { get; private set; } = 1000 * 3600;
        /// <summary>
        /// 是否输出流程节点信息
        /// </summary>
        public static bool OutputNodeInfo { get; private set; } =true;

    }
}
